package com.ycxy.crm.settings.web.interceptor;

import com.ycxy.crm.commons.contants.Contants;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author jayLuo
 * @Date 2022/9/21 10:24
 * @PackageName:com.ycxy.crm.settings.web.interceptor
 * @ClassName: LoginInterceptor
 * @Description: TODO
 * @Version 1.0
 */
public class LoginInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        //如果用户没有登录成功，则跳转到登录页面
        if(request.getSession().getAttribute(Contants.SESSION_USER) == null ){
            response.sendRedirect(request.getContextPath());//重定向时，url必须加项目名
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
